package com.genokon.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genokon.entry.GeneServiceBaseDict;
import com.genokon.entry.GeneServiceDoctor;
import com.genokon.entry.GeneServiceProduct;
import com.genokon.mapper.GeneServiceBaseDictMapper;
import com.genokon.mapper.GeneServiceDoctorMapper;
import com.genokon.mapper.GeneServiceProductMapper;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author by baichaoda
 * @date 2021/4/20 13:44
 */
@RestController
@RequestMapping("/dict")
public class DictController {
    @Resource
    private GeneServiceBaseDictMapper geneServiceBaseDictMapper;
    @Resource
    private GeneServiceDoctorMapper geneServiceDoctorMapper;
    @Resource
    private GeneServiceProductMapper geneServiceProductMapper;

    /**
     * 字典
     * @param dict
     * @return
     */
    @GetMapping("/list")
    R<List<GeneServiceBaseDict>> getDictList(String dict){
        R<List<GeneServiceBaseDict>> result;
        try {
            QueryWrapper<GeneServiceBaseDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("dict_code", dict);
            queryWrapper.orderByAsc("code");
            Map<String, Object> param = new HashMap<>(1);
            param.put("dict_code", dict);
            result = R.ok(geneServiceBaseDictMapper.selectList(queryWrapper));
        } catch (Exception e) {
            result = R.failed(e.getMessage());
        }
        return result;
    }

    /**
     * 分页医生信息（只能选择当前销售的）
     * @param org 机构
     * @param doctor 医生
     * @param sale 销售
     * @param page 当前页
     * @return R
     */
    @GetMapping("/doctor")
    R<List<GeneServiceDoctor>> getDictList(String org, String doctor, String sale, int page){
        if(StringUtils.isEmpty(sale)){
            return R.failed("销售参数【sale】必须存在！");
        }
        R<List<GeneServiceDoctor>> result;
        try {
            result = R.ok(geneServiceDoctorMapper.fineDoctorList(new Page<>(page, 10), sale, doctor, org));
        } catch (Exception e) {
            result = R.failed(e.getMessage());
        }
        return result;
    }

    /**
     * 分页医生信息（只能选择当前销售的）
     * @param page 当前页
     * @return R
     */
    @GetMapping("/product")
    R<List<GeneServiceProduct>> getProductList(String name, int page){
        R<List<GeneServiceProduct>> result;
        try {
            QueryWrapper<GeneServiceProduct> queryWrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(name)){
                queryWrapper.like("name", name);
            }

            Page<GeneServiceProduct> pageObject = new Page<>(page, 10);
            Page<GeneServiceProduct> rProduct = geneServiceProductMapper.selectPage(pageObject, queryWrapper);
            result = R.ok(rProduct.getRecords());
        } catch (Exception e) {
            result = R.failed(e.getMessage());
        }
        return result;
    }
}
